<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* class control authorisation
	Author : Iswan Putera
	
*/

class Zetro_auth //extends CI_Controller
{
	function __construct()
	{
		//parent::__construct();
		$this->CI=& get_instance();
		$this->CI->load->model('main_model');	
		$this->CI->load->library('zetro_form');	
		$this->u_login=$this->CI->session->userdata('userid');
		$this->u_group=$this->CI->session->userdata('user_group');
	}
	
	function menus($menu)
	{
		$this->menu=$menu;	
	}
	
	function cek_oto($field)
	{
		$this->CI->main_model->table_name('user_oto');
		$this->CI->main_model->set_methods('select');
		$this->CI->main_model->where(array('userid'=>$this->u_group,'modul'=>$this->menu,$field=>'Y'));
		$this->CI->main_model->query($field);
		$data=$this->CI->main_model->set_data();
		if($data)
		{
			return 'Checked';
		}else{
			return ($this->u_group=='Superuser')?'Checked':'';
		}
	}
	
	function allAuth($data,$menu)
	{
		$this->menus($menu);
		if($this->cek_oto('v')=='' &&
		   $this->cek_oto('e')=='' &&
		   $this->cek_oto('c')=='' ){
			   return ($this->u_group=='Superuser')?$data:$this->noAuth();
		   }else{
			   return $data;
		   }
			 
	}
	function noAuth()
	{
		return "<i class='cus-group-error'></i> You have not authorization, please contact your administrator";
	}
	//user area authorization
	function cek_area(){
		$data=array();$area='';$n=0;
		$this->CI->main_model->table_name('user_oto_area');
		($this->u_group!='Superuser')?
		$this->CI->main_model->where(array('userid'=>$this->u_login,'c'=>'Y')):
		$this->CI->main_model->query();
		$this->CI->main_model->set_methods('select');
		$data=$this->CI->main_model->set_data();
		
		foreach($data as $r){
			$n++;
			$koma=($n==count($data))?"":",";
			$area[]=$r->lokasi;	
		}
		return $area;
		//print_r($area);
	}
	function cek_approved()
	{
		return rdb('users','approval','approval',"where userid='".$this->session->userdata('userid')."'");	
	}

	//process logging user
	function user_log($stat='')
	{
		if($stat=='')
		{
			$data['userid']=$this->CI->session->userdata('userid');
			$data['ipaddress']=$_SERVER['REMOTE_ADDR'];
			$data['accessmodul']='home';
			$this->CI->main_model->prepared_data('user_log',$data);
			$this->CI->main_model->set_data();
		}else if ($stat=='logout'){
			$data['userid']=$this->CI->session->userdata('userid');
			$data['logouttime']=date('Y-m-d H:s:m');
			$this->CI->main_model->prepared_data('user_log',$data,'update');
			$this->CI->main_model->where(array('userid'=>$this->CI->session->userdata('userid')));
			$this->CI->main_model->set_data();	
		}else{
			$data['userid']=$this->CI->session->userdata('userid');
			$data['accessmodul']=$stat;
			$this->CI->main_model->prepared_data('user_log',$data,'update');
			$this->CI->main_model->where(array('userid'=>$this->CI->session->userdata('userid')));
			$this->CI->main_model->set_data();	
		}
	}
	/**
		user logout destroy all session 
		before destroy session update user_log
	*/
	function log_out()
	{
		$this->user_log('logout');
        $data = array
            (
            'userid' 	=> 0,
            'username'	=> 0,
            'type' 		=> 0,
			'idlevel'	=> 0,
            'login' 	=> FALSE,
			'version'	=> '',
			'gudang'	=>''
        );
        $this->CI->session->sess_destroy();
        $this->CI->session->unset_userdata($data);
		header('Location :'.base_url());
	}
	/**
		get login status from cookies name login
	*/
	function check_login()
	{
		($this->CI->session->userdata('login')!=TRUE)?
		header('Location:'.base_url()):header('Location'.base_url().'admin/index');	
	}
	/**
	genarate table for  user for first run application / on setup process
	*/
	function _generate_user_table()
	{
		

		$sql="CREATE TABLE IF NOT EXISTS `users` (
			  `userid` varchar(50) NOT NULL,
			  `username` varchar(150) DEFAULT NULL,
			  `password` varchar(50) DEFAULT NULL,
			  `nm_group` varchar(50) DEFAULT NULL,
			  `active` enum('Y','N') DEFAULT 'Y',
			  `createdate` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
			  PRIMARY KEY (`userid`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;";

		$sql1="CREATE TABLE IF NOT EXISTS `user_group` (
			  `nm_group` varchar(50) NOT NULL,
			  `created_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
			  PRIMARY KEY (`nm_group`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;";

		$sql2="CREATE TABLE IF NOT EXISTS `user_oto` (
			  `userid` varchar(50) NOT NULL,
			  `modul` varchar(150) NOT NULL,
			  `c` enum('Y','N') DEFAULT 'N',
			  `e` enum('Y','N') DEFAULT 'N',
			  `v` enum('Y','N') DEFAULT 'N',
			  `p` enum('Y','N') DEFAULT 'N',
			  PRIMARY KEY (`modul`,`userid`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
		$sql5="CREATE TABLE IF NOT EXISTS `user_log` (
				`id` INT NULL AUTO_INCREMENT,
				`userid` VARCHAR(50) NULL,
				`ipaddress` VARCHAR(50) NULL,
				`logintime` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
				`logouttime` DATETIME NULL,
				`accessmodul` VARCHAR(150) NULL,
				PRIMARY KEY (`id`)
			)
			COLLATE='latin1_swedish_ci'
			ENGINE=InnoDB;";

	mysql_query($sql);	mysql_query($sql1);	mysql_query($sql2);mysql_query($sql5);
	//-- --------------------------------------------------------
	

		$sql3="INSERT INTO users (userid, username,password,nm_group,active,createdate) VALUES
			('superuser', 'Superuser', '8b5e22ec5b6020776fc7f5ec4897ac52', 'Superuser', 'Y', '2012-10-01 04:05:01')";

		$sql4="INSERT INTO user_group (nm_group) VALUES
			('Superuser'),
			('Administrator')";
	
	//check data 	
		$this->CI->main_model->query();
		$this->CI->main_model->table_name('users');
		$this->CI->main_model->set_methods('select');
		$this->CI->main_model->where(array('userid'=>'superuser'));
		$data=$this->CI->main_model->set_data();
		if(!$data)
			{
				mysql_query($sql3) or die(mysql_error());
				mysql_query($sql4) or die(mysql_error());

			}

	}
}
